package com.ls.controller.customer;


import com.ls.controller.customer.model.*;
import com.ls.entity.Customer;
import com.ls.entity.SysUser;
import com.ls.service.ICustomerService;
import com.ls.utils.FileUtils;
import com.ls.utils.ResultData;
import com.ls.utils.login.CurrentUser;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CustomerController
 * @Description 用户控制器
 * <p>
 * C端用户表 前端控制器
 * </p>
 *
 * @author pengyu
 * @since 2023-03-15
 * 由于本项目的token从 "token"  改成了 "Authorization" ，所以这里也要改 查询官网看看这里
 * 怎么正常返回Authorization
 */
@Api(value = "用户相关接口", tags = "用户相关接口")
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
public class CustomerController {
    private final ICustomerService service;
//    @Resource(name = "redisTemplate")
//    private RedisTemplate<String,Object> redisTemplate;

    private final RedisTemplate<String,Object> redisTemplate;

    /** 查询C端用户列表  */
    @GetMapping(value = "getPaging")
    public ResultData getPaging(QueryCustomerModel model) {
        return service.getPaging(model);
    }

    /*** 按查询条件导出查询C端用户列表 */
    @PostMapping(value = "export")
    @ResponseBody
    public void export(@Valid @RequestBody QueryCustomerModel model, HttpServletResponse response) {
        service.export(model, response);
    }

    /** 通过ID用户查询C端用户详情  */
    @GetMapping(value = "getInfoById")
    public ResultData getInfoById(@RequestParam Long customerId) {
        return service.getInfoById(customerId);
    }

    /** 通过ID更新C端用户标签  @PathVariable 也是常用的注解*/
    @PostMapping(value = "updateTradeById")
    public ResultData updateTradeById(@Valid @RequestBody UpdateTradeModel model) {
        return service.updateTradeById(model);
    }

    //TDOO 查询登录记录放在 login_record 表对应的 controller里面 来写



    /** 导入用户 */
    @PostMapping("/uploadCustomer")
    public ResultData uploadCustomer(@RequestParam("file") MultipartFile file) {
        File dest = FileUtils.createTempFile(file);
        //解析文件
        ResultData<List<Customer>> analyseResult = service.analyseExcelCustomer(dest);
        if (!analyseResult.whetherSuccess()) {
            return analyseResult;
        }
        //入库  service.saveBatch  改成mapper一条一条来插入，然后记录成功，失败，详情等等
        List<Customer> list = analyseResult.getData();
        return service.saveUploadCustomer(list);
    }

    @GetMapping("/redisTest")
    public String redis() {
        redisTemplate.opsForValue().set("name","neo",10, TimeUnit.SECONDS);
        String name = (String) redisTemplate.opsForValue().get("name");
        return name;
//        LettuceRedisUtils.set("ttttt", LocalDateTime.now().toString());
//        String name = LettuceRedisUtils.get("ttttt");
//        return name;
    }
    /** 签名列表 */
    @GetMapping("/getSignList")
    public ResultData getSignList(QuerySignModel model) {
        return service.getSignList(model);
    }

    /** 签名状态变更 */
    @PostMapping("/updateStatus")
    public ResultData updateStatus(@Valid @RequestBody UpdateStatusModel model) {
        return service.updateStatus(model);
    }

    /** 设置签名  */
    @PostMapping(value = "setSign")
    public ResultData setSign(@Valid @RequestBody SetSignModel model) {
        return service.setSign(model);
    }

    /** 统计信息  */
    @GetMapping(value = "getData")
    public ResultData getData(TimeModel model) {
        return service.getData(model);
    }
    /** 统计信息会员年龄  */
    @GetMapping(value = "getDataAge")
    public ResultData getDataAge(TimeModel model) {
        return service.getDataAge(model);
    }
    /** 统计信息地区会员  */
    @GetMapping(value = "getDataMap")
    public ResultData getDataMap(TimeModel model) {
        return service.getDataMap(model);
    }
    /** 统计信息地区会员列表  */
    @GetMapping(value = "getDataMapList")
    public ResultData getDataMapList(TimeModel model) {
        return service.getDataMapList(model);
    }
}

